// 做题 1 实现 get 方法
// var obj = {a: {b: {c: 2}}}; console.log(get(obj, 'a.b.c')); // 输出 2
function get(obj, path) {
	const keys = path.split('.')
	let result = obj
	for (const key of keys) {
		if (result) {
			result = result[key]
		} else {
			return undefined
		}
	}
	return result
}

// 测试
const obj = { a: { b: { c: 2 } } }
console.log(get(obj, 'a.b.c')) // 输出 2
// console.log(get(obj, 'a.b.d')); // 输出 undefined
// console.log(get(obj, 'a.b')); // 输出 { c: 2 }
// const fn = (arr, count) => {
//   let result = [];
//   let temp = [];
//   for (let i = 0; i < arr.length; i++) {
//     temp.push(arr[i]);
//     if (temp.length === count) {
//       result.push(temp);
//       temp = [];
//     }
//     console.log('result', result);
//   }
//   if (temp.length > 0) {

//     result.push(temp);
//   }

//   return result;
// }
// 做题 2 把数组平分，实现 fn
// fn([1, 2, 3, 4, 5], 2) //结果为[[1,2],[3,4],[5]]
function fn(arr, count) {
	let result = []
	for (let i = 0; i < arr.length; i += count) {
		result.push(arr.slice(i, i + count))
	}
	return result
}

function findMaxRepeatedChar(str) {
	debugger
	if (str.length === 0) return null

	let maxChar = ''
	let maxStart = 0
	let maxEnd = 0
	let maxCount = 0

	let currentStart = 0

	for (let i = 1; i <= str.length; i++) {
		if (i === str.length || str[i] !== str[currentStart]) {
			const currentCount = i - currentStart
			if (currentCount > maxCount) {
				maxChar = str[currentStart]
				maxStart = currentStart
				maxEnd = i - 1
				maxCount = currentCount
			}
			currentStart = i
		}
	}

	return {
		char: maxChar,
		start: maxStart,
		end: maxEnd
	}
}

// 测试示例
const result = findMaxRepeatedChar('aabbbccddddddeff')
console.log(result) // { char: 'd', start: 8, end: 13 }

console.log('ww', fn([1, 2, 3, 4, 5], 2)) //结果为[[1,2],[3,4],[5]]

// 两数之和
// 排序： 创建指针 找到终止条件
const toSum = (arr, target) => {
	// 排序
	const sortNum = Array.from(arr).sort((a, b) => a - b)
	// 创建指针
	let left = 0
	right = arr.length - 1
	// 对撞-》找到终止条件
	while (left < right) {
		const a = sortNum[left]
		const b = sortNum[right]
		const currentSum = a + b
		if (target === currentSum) {
			return [arr.indexOf(a), arr.indexOf(b)]
		} else if (target < currentSum) {
			right--
		} else {
			left++
		}
	}
}

console.log(toSum([1, 4, 3, 12, 5], 9))
// 最接近的三数之和
/**
 *
 * @param {数据源} arr
 * @param {目标值} target
 * @returns
 */
const threeSumClosest = (arr, target) => {
	if (arr.length === 3) {
		return arr.reduce((acc, pre) => acc + pre, 0)
	}
	arr.sort((a, b) => a - b)
	let closeSetSum = Infinity
	for (let i = 0; i < arr.length - 2; i++) {
		let left = i + 1
		let right = arr.length - 1
		while (left < right) {
			let currentSum = arr[i] + arr[left] + arr[right]
			if (currentSum === target) {
				return target
			}
			if (Math.abs(currentSum - target) < Math.abs(closeSetSum - target)) {
				closeSetSum = currentSum
			}
			if (currentSum > target) {
				right--
			} else {
				left++
			}
		}
	}
	return closeSetSum
}

console.log(threeSumClosest([1, 2, 4, 8, 16, 32, 64, 128], 82))

console.log(threeSumClosest([-1, 2, 1, -4], 1)) // 输出: 2
console.log(threeSumClosest([1, 2, 3], 3)) // 输出: 2

// 分别创建两个指针， 开始遍历长的字符串，如果两个指针字符串相同，两个指针都移动，
// 不相同，移动长字符串指针
// 终止条件， 短字符串走完，指针和短字符串长度不一致， 结束
// 如果长字符串走完，false
// 判断子序列
const isSubsequence = (s, t) => {
	let sLen = s.length
	if (!sLen) return false
	let i = 0
	let j = 0
	while (j < t.length) {
		if (s[i] === t[j]) {
			i++
		}
		j++
		if (i === sLen) {
			return true
		}
	}
	return i === sLen
}
console.log(isSubsequence('abc', 'ahbgdc')) // true
console.log(isSubsequence('axc', 'ahbgdc')) // false
console.log(isSubsequence('ace', 'abcde')) // true
console.log(isSubsequence('', 'abcde')) // true (空字符串是任何字符串的子序列)
console.log(isSubsequence('abc', '')) // false (非空字符串不可能是空字符串的子序列)
// 字符串/链表/数组的关系 快慢指针
// 零移动
/**
 * 将数组中的所有零元素移动到末尾，同时保持非零元素的相对顺序不变
 * @param {number[]} nums
 * @return {void} 不返回任何内容，直接修改输入数组
 */
function moveZeroes(arr) {
	let slow = 0
	for (let fast = 0; fast < arr.length; fast++) {
		if (arr[fast] !== 0) {
			// 交换 arr[slow] 和 arr[fast]
			;[arr[slow], arr[fast]] = [arr[fast], arr[slow]]
			slow++
		}
	}
	return arr
}
/**
 * 1，0，0，3，12
 */
// 测试用例
let arr1 = [0, 1, 0, 3, 12]
console.log(moveZeroes(arr1)) // 输出: [1, 3, 12, 0, 0]
// moveZeroes(arr1);

let arr2 = [0, 0, 1]
moveZeroes(arr2)
console.log(arr2) // 输出: [1, 0, 0]

let arr3 = [2, 1]
moveZeroes(arr3)
console.log(arr3) // 输出: [2, 1]
// 滑动窗口
